﻿ var qutoImgUrl = "";
 var qutoVideoUrl = "";
 function ShowMsgTxt() {
     var topicName = "";
     var name = $("#topicName").val();
     if (name != "") {
         $("#msgTxt").val("");
         topicName = "#" + name + "#"
     }
     $("#msgTxt").val(topicName);
     return topicName;
 }
 ShowMsgTxt();
 function addWeiBo() {
     var retVal = PUBAjaxClass.isUserBlcok($("#hidUserID").val()).value;
     if (retVal == 1) {
         art.dialog.alert("你已经被暂时禁言，请注意发表的内容！");
         return false;
     }
     var content = $("#msgTxt").val();
     if (content == "") {
         art.dialog.alert("请输入内容！");
         return false;
     }
     content = safeHtml(content);
     if (Ucenter.bTinyblog.weiBoBolgUrl != "") {
         var tempArr = Ucenter.bTinyblog.weiBoBolgUrl.split("/");
         if (Ucenter.bTinyblog.weiBoBolgUrl.indexOf("?") != -1) {
             Ucenter.bTinyblog.weiBoBolgUrl = tempArr[tempArr.length - 1].split('=')[1];
         } else {
             Ucenter.bTinyblog.weiBoBolgUrl = tempArr[tempArr.length - 1];
         }
         content += "@PMH_M_BLOG=" + Ucenter.bTinyblog.weiBoBolgUrl;
     }
     if (Ucenter.bTinyblog.weiBoImgUrl != "") {
         var tempArr2 = Ucenter.bTinyblog.weiBoImgUrl.split("/");
         if (Ucenter.bTinyblog.weiBoImgUrl.indexOf("pic.") != -1 && Ucenter.bTinyblog.weiBoImgUrl.indexOf("/photos/") != -1) {
             content += "@PMH_M_IMAGEURL=" + Ucenter.bTinyblog.weiBoImgUrl;
         }
         else {
             if (Ucenter.bTinyblog.weiBoImgUrl.indexOf("?") != -1) {
                 Ucenter.bTinyblog.weiBoImgUrl = tempArr2[tempArr2.length - 1].split('=')[1];
             }
             else {
                 Ucenter.bTinyblog.weiBoImgUrl = tempArr2[tempArr2.length - 1];
             }
             content += "@PMH_M_IMAGE=" + Ucenter.bTinyblog.weiBoImgUrl;
         }
     }
     if (Ucenter.bTinyblog.weiBoVideoUrl != "") {
         content += "@PMH_W_VIDEOURL=" + Ucenter.bTinyblog.weiBoVideoUrl;
         var tempArr3 = Ucenter.bTinyblog.weiBoVideoUrl.split("/");
         Ucenter.bTinyblog.weiBoVideoUrl = tempArr3[tempArr3.length - 1];
         if (Ucenter.bTinyblog.weiBoVideoUrl.indexOf("?") != -1) {
             Ucenter.bTinyblog.weiBoVideoUrl = Ucenter.bTinyblog.weiBoVideoUrl.split('?')[0];
         } else if (Ucenter.bTinyblog.weiBoVideoUrl.indexOf("_") != -1) {
             Ucenter.bTinyblog.weiBoVideoUrl = Ucenter.bTinyblog.weiBoVideoUrl.split('_')[0];
         }
         content += "@PMH_W_VIDEO=" + Ucenter.bTinyblog.weiBoVideoUrl;
     }
     var num = PUBAjaxClass.AddUserWeiBo2($("#hidUserID").val(),content, $("#hidTopicID").val()).value;
     if (num == 1) {
         art.dialog({
             content: "发布成功！",
             fixed: true,
             skin: 'facebook',
             icon: 'succeed',
             lock: false,
             effect: false,
             border: false
         }).follow("SendBtn").time(1);
         var weiboNum = $("#hidWeiboNum").val();
         if (weiboNum == "1") {
             $("#weiboNum").hide();
          }
         var content = ShowWeiboList(1, $("#hidUserID").val(), $("#hidTopicID").val());
         $('#viewContent').empty();
         content = $(content).prependTo($('#viewContent'));
         Ucenter.bTinyblog.init(content);
     }
     else if (num == -2) {
         art.dialog({
             content: "你已经被暂时禁言，请注意发表的内容！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).follow("SendBtn").time(1);
     }
     else {
         art.dialog({
             content: "出错了，请稍后再试！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).follow("SendBtn").time(1);
     }
     ShowMsgTxt();
 }

 function ReplyFill(content) {
     var userInfo = PUBAjaxClass.GetUserCardInfo($("#hidUserID").val()).value;
     var josn = eval(userInfo)[0];
    // userInfo = eval("{ret:1}");
     var tempHtml = "<li><div class=\"user_bar\"><div class=\"avatar\"><a href=\"userhome.aspx?uid=" + $("#hidUserID").val() + "\"  target=\"_blank\"><img src=\"" + josn.avatar + "\" alt=\"" + josn.name + "\"></a></a></div></div>";
     tempHtml += "<div class=\"msgbox\"><span class=\"username\"><a href=\"userhome.aspx?uid=" + $("#hidUserID").val() + "\">" + josn.name + "：</a></span>";
     tempHtml += "<p class=\"msgcon\">" + content + "</p>";
     tempHtml += "</div></li>";
     $("#viewContent").prepend(tempHtml);
 }
 function SubscibeBlog(curUserID, blogUserID) {
     var retVal = PUBAjaxClass.SubscibeBlog(curUserID, blogUserID).value;
     if (retVal == "1") {
         $('#poper-usercard_' + curUserID).dialog({
             content: "关注成功！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).time(1);
     } else if (retVal == "-1") {
         $('#poper-usercard_' + curUserID).dialog({
             content: "关注失败！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).time(1);
     } else if (retVal == "-2") {
         $('#poper-usercard_' + curUserID).dialog({
             content: "该用户您已关注！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).time(1);
     } else if (retVal == "-3") {
         $('#poper-usercard_' + curUserID).dialog({
             content: "当前状态无法关注！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).time(1);
     }
 }
 //评论
 function CommentWeiBo(content, userID, noteId) {
     var ret = PUBAjaxClass.isUserBlcok($("#hidUserID").val()).value;
     if (ret == 1) {
         art.dialog.alert("你已经被暂时禁言，请注意发表的内容！");
         return false;
     }
     var userInfo = PUBAjaxClass.GetUserCardInfo($("#hidUserID").val()).value;
     var josn = eval(userInfo)[0];
     var retVal = PUBAjaxClass.CommentUserWeiBo(userID, josn.name, noteId, content).value;
     if (retVal == 1) {
         CreateCommentList(noteId,1)
     }
     else {
         art.dialog({
             content: "评论失败！",
             skin: 'facebook',
             lock: false,
             effect: false,
             border: false
         }).time(1);
     }
 }
 //转发
 function RelayUserWeiBo(content, userID, noteId) {
     var ret = PUBAjaxClass.isUserBlcok($("#hidUserID").val()).value;
     if (ret == 1) {
         art.dialog.alert("你已经被暂时禁言，请注意发表的内容！");
         return false;
     }
     var topicID = $("a[noteID="+noteId+"]").attr("topicID");
     var contentStr = ShowMsgTxt() + content;
     if (ShowMsgTxt() == "") {
         if(topicID != "0") {
             contentStr = "#"+PUBAjaxClass.GetTopicName(topicID).value+"#"+content;
         }
     }
     var retVal = PUBAjaxClass.RelayUserWeiBo2(userID, noteId, contentStr, topicID).value;
     $('.talk_pannel .controlbar .zf[noteid='+noteId+']').dialog({
         content: "转发成功！",
         fixed: true,
         skin: 'facebook',
         icon: 'succeed',
         lock: false,
         effect: false,
         border: false
     }).time(1);
     window.location.reload();
 }
 function CreateCommentList(noteId, curPage) {
    //数据加载后插入到层中
     var tempHtml = PUBAjaxClass.GetCommnetList(noteId, curPage).value;
     $("#comment_" + noteId).empty().html(tempHtml);
 }
 function CreateReplyList(noteId, curPage) {
     //数据加载后插入到层中
     var tempHtml = PUBAjaxClass.GetReplyList(noteId, curPage).value;
     $("#Reply_" + noteId).empty().html(tempHtml);
 }
 //删除微博
 function delWeiBo(userID, noteId) {
     var topicID = parseInt($("#hidTopicID").val());
     var retVal = PUBAjaxClass.DeleteUserWeiBo2(userID, noteId, topicID).value;
 }
 function DelSubscibeBlog(curUserID,userID) {
     var retVal = PUBAjaxClass.DelFlowUser(curUserID, userID).value;
     $('#poper-usercard_' + curUserID).dialog({
         content: "取消成功！",
         skin: 'facebook',
         lock: false,
         effect: false,
         border: false
     }).time(1);
 }

 //插入话题功能
 $(document).ready(function () {
     $("#topic").click(function () {
         inTopic("msgTxt", "请输入话题");
     });
 });
 //插入话题
 function inTopic(id, str) {
     var str = str || "请在这里输入自定义话题";
     str = "#" + str + "#";
     var reg = new RegExp(str),
	oTextarea = document.getElementById(id),
	oValue = oTextarea.value,
	val_index = 0,
	val_match,
    val_pos;
     if (reg.test(oValue)) {
         val_match = oValue.match(reg);
         val_index = val_match.index;
     }
     else {
         val_pos = getTextPos(oTextarea);
         val_index = val_pos.start;
         rangeText(oTextarea, str);
     }
     selectText(oTextarea, val_index + 1, val_index + str.length - 1);
 }
 //光标的当前位置
 function getTextPos(obj) {
     if (!obj || obj.offsetWidth == 0) { return false; }
     var t = obj, start = 0, end = 0, value = '', range, range_all;
     value = t.value;
     if (typeof (t.selectionStart) == "number") {
         start = t.selectionStart;
         end = t.selectionEnd;
     }
     else if (document.selection && t.offsetWidth > 0) {
         t.focus();
         range = document.selection.createRange();
         if (range.parentElement() == t) {
             range_all = document.body.createTextRange();
             range_all.moveToElementText(t);
             for (start = 0; range_all.compareEndPoints("StartToStart", range) < 0; start++) {
                 range_all.moveStart('character', 1);
             }
             for (var i = 0; i <= start; i++) {
                 if (t.value.charAt(i) == '\n') {
                     start++;
                 }
             }
             range_all.moveToElementText(t);
             for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end++) {
                 range_all.moveStart('character', 1);
             }
             for (var i = 0; i <= end; i++) {
                 if (t.value.charAt(i) == '\n') {
                     end++;
                 }
             }
         }
     }
     else {
         start = value.length;
         end = value.length;
     }
     return {
         start: start,
         end: end
     }
 }
 //光标处替换的文本
 function rangeText(obj, val, num) {
     if (!obj || obj.offsetWidth == 0) { return false; }
     var t = obj, start = 0, end = 0, value = '', val_start = '', val_end = '', pos;
     var userAgent = navigator.userAgent.toLowerCase(), ie = /msie/.test(userAgent) && !/opera/.test(userAgent);
     if (ie && t.nodeName.toLocaleLowerCase() != 'textarea') {
         t.value = val;
         t.focus;
         return;
     }
     value = t.value;
     pos = getTextPos(t);
     start = pos.start;
     end = pos.end;
     if (typeof num == 'number') {
         start = start - num;
     }
     val_start = value.substring(0, start);
     val_end = value.substring(end);
     t.value = val_start + val + val_end;
     if (typeof num == 'number') {
         end = end - num;
     }
     selectText(t, end + val.length, end + val.length);
 }
 //光标定位到指定文本
 function selectText(obj, start, stop) {
     if (!obj || obj.offsetWidth == 0) { return false; }
     var range;
     if (obj.setSelectionRange) {
         obj.setSelectionRange(start, stop);
     }
     else {
         range = obj.createTextRange();
         range.collapse(true);
         range.moveStart("character", start);
         range.moveEnd("character", stop - start);
         range.select();
     }
     obj.focus();
 }

 //可能认识的人
 function AjaxAddFriendMayKnow(userID, friendID) {
     if (friendID <= 0 || friendID == "") {
         art.dialog.alert("好友ID不能为空");
         return;
     }
     $.getJSON("http://interface.baomihua.com/pubapi.asmx/AddFriend?jsoncallback=?", { uid: friendID }, function (json) {
         if (typeof (json.Result) != 'undefined') {
             iRet = json.Result;
             switch (iRet) {
                 case "4":
                     art.dialog({
                         content: "他已经是你的好友了！",
                         skin: 'facebook',
                         lock: false,
                         effect: false,
                         border: false
                     }).time(1);
                     break;
                 case "-9":
                     art.dialog({
                         content: "你不能成为自己的好友！",
                         skin: 'facebook',
                         lock: false,
                         effect: false,
                         border: false
                     }).time(1);
                     break;
                 case "-3":
                     art.dialog({
                         content: "用户状态无效！",
                         skin: 'facebook',
                         lock: false,
                         effect: false,
                         border: false
                     }).time(1);
                     break;
                 case "-2":
                     art.dialog({
                         content: "你未登录，请先登录再添加好友！",
                         skin: 'facebook',
                         lock: false,
                         effect: false,
                         border: false
                     }).time(1);
                     break;
                 case "-1", "0":
                     art.dialog({
                         content: "出现错误，请稍后再试！",
                         icon: 'error',
                         skin: 'aero',
                         yesFn: true,
                         width: 200,
                         height: 60
                     }).time(1);
                     break;
                 case "1":
                     art.dialog({
                         content: "加好友成功",
                         skin: 'facebook',
                         lock: false,
                         effect: false,
                         border: false
                     }).time(1);
                     var type = $("#hidType").val();
                     if (type == "1") {
                         var conent = PUBAjaxClass.GetMayKnowHTML(userID, 1).value;
                         $("#noteBookID").html(conent);
                         var ret = PUBAjaxClass.GetUserFollow_NoteBook_Tool(userID, friendID).value;
                         var content = ShowWeiboList(1, $("#hidUserID").val(), $("#hidTopicID").val());
                         $('#viewContent').empty();
                         content = $(content).prependTo($('#viewContent'));
                         Ucenter.bTinyblog.init(content);
                     }


                     break;
             }
         }
     });

 }